/**
 * reduce() 方法对数组中的每个元素按序执行一个提供的 reducer 函数，
 * 每一次运行 reducer 会将先前元素的计算结果作为参数传入，最后将其结果汇总为单个返回值。
 */
const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  initialValue,
);
// Expected output: 10

console.log(sumWithInitial);

console.log(array1.reduce(
    (accumulator, currentValue) => accumulator + currentValue,
  ))
// Expected output: 10


/**
 * reduceRight
 */
const array2 = [
  [0, 1],
  [2, 3],
  [4, 5],
];

const result = array2.reduceRight((accumulator, currentValue) =>
  accumulator.concat(currentValue),
);

console.log(result);
// Expected output: Array [4, 5, 2, 3, 0, 1]
